package com.program.cloudmanage.tripteam.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.program.cloudmanage.tripteam.domain.*;
import com.program.cloudmanage.tripteam.service.*;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.http.DefaultCosHttpClient;
import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.utils.IOUtils;
import com.thoughtworks.xstream.mapper.Mapper;
import org.apache.http.HttpException;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

@Component
public class Schedule {
    @Autowired
    private TripTeamService tripTeamService;
    @Autowired
    private ItineraryService itineraryService;

    @Scheduled(fixedRate = 10000)
    public void test() throws Exception {
        COSClient cosClient = tripTeamService.createCOSClient();
        String bucketName = "red3and-1325710075";
        int a = 1;
        int c = 100*a;
        while (true) {
            String Key = Integer.toString(a);
            String Key2 = Integer.toString(c);
            a = a + 1;
            Boolean objectExists = null;

            try {
                objectExists = cosClient.doesObjectExist(bucketName, Key);
            } catch (CosServiceException e) {
                System.out.println("AAA");
            }
            JSONObject jsonObject1 = null;
            if (objectExists == true && itineraryService.searchItinerary(Key) == null) {
                COSObject cosObject = cosClient.getObject(new GetObjectRequest(bucketName, Key));
                InputStream cosObjectInput = cosObject.getObjectContent();
                byte[] bytes = IOUtils.toByteArray(cosObjectInput);
                String b = new String(bytes);
                jsonObject1 = JSON.parseObject(b);
                System.out.println(jsonObject1);

                Itinerary itinerary = new Itinerary();
                itinerary.setItineraryid(Key);
                itinerary.setPersonid(jsonObject1.getString("personid"));
                itinerary.setTeamid(jsonObject1.getString("teamid"));
                itinerary.setStartpoint(jsonObject1.getString("startposition"));
                itinerary.setTerminal(jsonObject1.getString("endposition"));
                itinerary.setSimilarity(jsonObject1.getString("similarity"));
                itinerary.setWayposition(jsonObject1.getString("wayposition"));

                COSObject cosObject2 = cosClient.getObject(new GetObjectRequest(bucketName, Key2));
                InputStream cosObjectInput2 = cosObject2.getObjectContent();
                byte[] bytes2 = IOUtils.toByteArray(cosObjectInput2);
                String d = new String(bytes2);

                itinerary.setPass(d);
                itineraryService.insertItinerary(itinerary);


            } else if (objectExists == true && itineraryService.searchItinerary(Key) != null) {
                COSObject cosObject = cosClient.getObject(new GetObjectRequest(bucketName, Key));
                InputStream cosObjectInput = cosObject.getObjectContent();
                byte[] bytes = IOUtils.toByteArray(cosObjectInput);
                String b = new String(bytes);
                jsonObject1 = JSON.parseObject(b);

                String similarity = jsonObject1.getString("similarity");
                itineraryService.updateSimilarity(similarity, Key);
                System.out.println("succeed");
            } else if (a == 90) {
                break;
            }
//            cosClient.shutdown();
        }


        //cosClient.shutdown();




    }
}
